package dao;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;

public class BaseHibernateDao {
	public Session getSession() {
    	return HibernateSessionFactory.getSession();  
    }
  
	public void add(Object item){
		Transaction tx = null;
		Session session = HibernateSessionFactory.getSession();  
		try {
        	tx = session.beginTransaction();
        	session.save(item);
        	tx.commit();
        	session.close();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        }
	}
	
	public Object load(java.io.Serializable id,Class clazz){
		try { 
			Object item = getSession().get(clazz, id);
			return item;
        } catch (RuntimeException re) {
            throw re;
        }
	}
	
	public void del(java.io.Serializable id,Class clazz){
		try {
        	Transaction tx = getSession().beginTransaction();
        	getSession().delete(this.load(id, clazz));
        	tx.commit();
        } catch (RuntimeException re) {
            throw re;
        }		
	}
	
	public void update(Object item){
		try {
        	Transaction tx = getSession().beginTransaction();
        	getSession().update(item);
        	tx.commit();
        } catch (RuntimeException re) {
            throw re;
        }
	}
	
	/**
	 * 用于执行HQL语句的方法
	 * @param hql
	 * @return
	 */
	public List excuteHQL(String hql){
		Session session = getSession();
		Query query=session.createQuery(hql);
		return query.list();
	}
	
	/**
	 * 用于执行分页功能的HQL语句
	 * @param hql
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	public List excuteHQL(String hql,int startIndex,int pageSize){
		Session session = getSession();
		Query query=session.createQuery(hql);
		query.setFirstResult(startIndex);
		query.setMaxResults(pageSize);
		return query.list();
	}
	
	public List search(Object condition,Class clazz){
		try {
			Criteria criteria=getSession().createCriteria(clazz);
			criteria.add(Example.create(condition));
			List results=criteria.list();
            return results;
        } catch (RuntimeException re) {
            throw re;
        }
	}
	
	public List search(Object condition,Class clazz,int pageIndex,int pageSize){
		try {
			Criteria criteria=getSession().createCriteria(clazz);
			criteria.setFirstResult(pageIndex);
			criteria.setMaxResults(pageSize);
			criteria.add(Example.create(condition));
			List results=criteria.list();
            return results;
        } catch (RuntimeException re) {
            throw re;
        }
	}
	
	public List findObjectByProperty(String propertyName, Object value, Class clazz){
		try {
			Criteria criteria=getSession().createCriteria(clazz);
			criteria.add(Restrictions.eq(propertyName, value));
			List results=criteria.list();
			System.out.println("size:"+results.size());
            return results;
        } catch (RuntimeException re) {
            throw re;
        }
	}
}
